/*
 * Copyright (c) chao
 *
 * SPDX-License-Identifier: 
 *
 * Change Logs:
 * Date           Author       Notes
 * 2022-03-27     chao         first version
 */

#include "stdio.h"
#include "string.h"
#include "lcd.h"

/*
 * @brief private variables
 */
static lcd_t lcd;
static lcd_color_t (*frame_buff)[LCD_X_PIXEL];

/*
 * @brief private functions
 */

/*
 * @brief 
 * @param 
 * @retval 
 */
void lcd_init(void)
{
    lcd.pen_color = COLOR_BLACK;
    lcd.bk_color = COLOR_WHITE;
    lcd.x_size = LCD_X_PIXEL;
    lcd.y_size = LCD_Y_PIXEL;
    lcd.bpp = LCD_BPP;
}

/*
 * @brief  lcd»º´æÉèÖÃ
 * @param  
 * @retval 
 */
void lcd_set_frame_buff(void *fb)
{
    frame_buff = (lcd_color_t(*)[LCD_X_PIXEL])fb;
}

/*
 * @brief 
 * @param 
 * @retval 
 */
uint16_t lcd_get_x_size(void)
{
    return lcd.x_size;
}

/*
 * @brief 
 * @param 
 * @retval 
 */
uint16_t lcd_get_y_size(void)
{
    return lcd.y_size;
}

/*
 * @brief 
 * @param 
 * @retval 
 */
void lcd_set_bpp(uint8_t bpp)
{
    lcd.bpp = bpp;
}

/*
 * @brief 
 * @param 
 * @retval 
 */
uint8_t lcd_get_bpp(void)
{
    return lcd.bpp;
}

/*
 * @brief 
 * @param 
 * @retval 
 */
void lcd_set_pen_color(lcd_color_t color)
{
    lcd.pen_color = color;
}

/*
 * @brief 
 * @param 
 * @retval 
 */
lcd_color_t lcd_get_pen_color(void)
{
    return lcd.pen_color;
}

/*
 * @brief 
 * @param 
 * @retval 
 */
void lcd_set_bk_color(lcd_color_t color)
{
    lcd.bk_color = color;
}

/*
 * @brief 
 * @param 
 * @retval 
 */
lcd_color_t lcd_get_bk_color(void)
{
    return lcd.bk_color;
}

/*
 * @brief 
 * @param 
 * @retval 
 */
void lcd_write_pixel(uint16_t x, uint16_t y, lcd_color_t color)
{
    frame_buff[y][x] = color;
}

/*
 * @brief 
 * @param 
 * @retval 
 */
lcd_color_t lcd_read_pixel(uint16_t x, uint16_t y)
{
    return frame_buff[y][x];
}

/*
 * @brief 
 * @param 
 * @retval 
 */
void lcd_fill(uint16_t sx, uint16_t sy, uint16_t ex, uint16_t ey, lcd_color_t color)
{    
    uint16_t x,y;

    for (y=sy; y<=ey; y++)
    {
        for (x=sx; x<=ex; x++)
        {
            frame_buff[y][x] = color;
        }
    }
}

/*
 * @brief 
 * @param 
 * @retval 
 */
void lcd_draw_rectangle(uint16_t sx, uint16_t sy, uint16_t ex, uint16_t ey, lcd_color_t color, uint16_t pixel)
{
    lcd_fill(sx, sy, ex, sy+pixel-1, color);
    lcd_fill(sx, ey-pixel+1, ex, ey, color);
    lcd_fill(sx, sy, sx+pixel-1, ey, color);
    lcd_fill(ex-pixel+1, sy, ex, ey, color);
}

/*
 * @brief 
 * @param 
 * @retval 
 */
void lcd_clear(void)
{
    lcd_fill(0, 0, lcd.x_size-1, lcd.y_size-1, lcd.bk_color);
}


